<template>
  <div class="container">
    <el-button @click="createPoint" type="primary">点位测量</el-button>
    <el-button @click="createDistance" type="primary">距离测量</el-button>
    <el-button @click="createHeight" type="primary">高度测量</el-button>
    <el-button @click="createArea" type="primary">面积测量</el-button>
    <el-button @click="createSurfaceArea" type="primary">表面积测量</el-button>
    <el-button @click="startAnalysis" type="primary">表面积分析</el-button>
    <el-button @click="startViewAnalysis" type="primary">通视分析</el-button>
    <el-button @click="startViewShedAnalysis" type="primary">视域分析</el-button>
    <el-button @click="createDirection" type="primary">方位角测量</el-button>

    <el-button type="primary" @click="clearLately" :disabled="!(celiangData.length > 0)"
      >撤回测量</el-button
    >
    <el-button type="primary" @click="clearAll" :disabled="!(celiangData.length > 0)"
      >清除全部</el-button
    >
  </div>
</template>

<script setup>
// import { ElMessage } from 'element-plus' // 添加 ElMessage引入 已有自动引入插件,但是防止eslint报错
import { ref, inject, onUnmounted } from 'vue'
import {
  ESLocationMeasurement,
  ESDistanceMeasurement,
  ESHeightMeasurement,
  ESSurfaceAreaMeasurement,
  ESAreaMeasurement,
  ESDirectionMeasurement,
  ESVisibilityAnalysis,
  ESViewShed,
} from 'earthsdk3'
const objm = inject('objm')
//默认飞入点
objm.activeViewer.flyIn(
  [108.9608604961044, 34.219333831420315, 496.2818115519124],
  [283.4536508628332, -15.604358729456605, 0.0001065806760175409],
  3,
)
// 定义一个数组，用来存储对象便于销毁
const celiangData = ref([])
// 创建点位
function createPoint() {
  const celiangPoint = objm.createSceneObject(ESLocationMeasurement)
  if (!celiangPoint) return
  celiangData.value.push(celiangPoint)
  celiangPoint.editing = true
}

//距离测量
const createDistance = () => {
  const celiangDistance = objm.createSceneObject(ESDistanceMeasurement) //距离
  if (!celiangDistance) return
  celiangData.value.push(celiangDistance)
  celiangDistance.editing = true
}

//高度测量
const createHeight = () => {
  const celiangHeight = objm.createSceneObject(ESHeightMeasurement) //高度
  if (!celiangHeight) return
  celiangData.value.push(celiangHeight)
  celiangHeight.editing = true
}

//面积测量
const createArea = () => {
  const celiangArea = objm.createSceneObject(ESAreaMeasurement) //面积
  if (!celiangArea) return
  celiangData.value.push(celiangArea)
  celiangArea.editing = true
}
const celiangSurfaceArea = ref(null)
//表面积测量
const createSurfaceArea = () => {
  celiangSurfaceArea.value = objm.createSceneObject(ESSurfaceAreaMeasurement) //表面积
  if (!celiangSurfaceArea.value) return
  celiangData.value.push(celiangSurfaceArea.value)
  celiangSurfaceArea.value.editing = true
}
//表面积分析
const startAnalysis = () => {
  celiangSurfaceArea.value.start()
}
//ESVisibilityAnalysis 通视分析   视线分析，分析单点与多点间视线是否连通；
const startViewAnalysis = () => {
  const sceneObjectESVisibilityAnalysis = objm.createSceneObject(ESVisibilityAnalysis)
  if (!sceneObjectESVisibilityAnalysis) return
  sceneObjectESVisibilityAnalysis.visibleColor = [0, 1, 0, 1]
  sceneObjectESVisibilityAnalysis.invisibleColor = [1, 0, 0, 1]
  sceneObjectESVisibilityAnalysis.editing = true
}
//视域分析
const startViewShedAnalysis = () => {
  const sceneObjectESViewShed = objm.createSceneObject(ESViewShed)
  if (!sceneObjectESViewShed) return
  sceneObjectESViewShed.showFrustum = true
  sceneObjectESViewShed.far = 1000
  sceneObjectESViewShed.near = 10
  sceneObjectESViewShed.editing = true
}

//方位角测量
const createDirection = () => {
  const celiangDirection = objm.createSceneObject(ESDirectionMeasurement) //方位角
  if (!celiangDirection) return
  celiangData.value.push(celiangDirection)
  celiangDirection.editing = true
}

//撤回最近一次测量
const clearLately = () => {
  if (celiangData.value.length > 0) {
    // 获取数组最后一项（最近创建的警报）
    const lately = celiangData.value.pop() //1.删除数组中的最后一个元素 2.返回被删除的元素
    // 销毁该警报对象
    objm.destroySceneObject(lately)
    // destroySceneObject(sceneObject)  删除并销毁场景对象；
    // Name                 Type                Description
    // sceneObject          ESSceneObject<T>   传入需要删除的场景对象

    //destroyAllSceneObjects()    删除所有场景对象；

    // ElMessage({
    //   message: '已删除最近创建的测量',
    //   type: 'success',
    //   showClose: true, //信息关闭按钮
    //   grouping: true, //相同信息合并
    //   duration: 1000, //显示时间
    // })
  }
}
//清除全部测量
const clearAll = () => {
  celiangData.value.forEach((item) => {
    if (item) objm.destroySceneObject(item)
  })
  celiangData.value = []
}
onUnmounted(() => {
  clearAll()
})
</script>

<style lang="scss" scoped>
.container {
  position: absolute;
  top: 300px;
  width: 340px;
  padding: 10px;
  border-radius: 5px;
  background-color: rgba(29, 37, 45, 0.8);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  button {
    width: 155px;
    height: 40px;
    margin-left: 0;
  }
}
</style>
